Skip to content

Method: setAttributeLanguage(Field, String)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model.descriptors;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
18:
19: import java.lang.reflect.Field;
20: import java.net.URI;
21: import java.util.Collection;
22: import java.util.Collections;
23: import java.util.Objects;
24: import java.util.Set;
25:
26: /**
27: * Describes a singular data property or a plural data, object or annotation property field.
28: */
29: public class FieldDescriptor extends Descriptor {
30:
31: private final Field field;
32:
33: public FieldDescriptor(Field attribute) {
34: this.field = Objects.requireNonNull(attribute);
35: }
36:
37: public FieldDescriptor(URI context, Field attribute) {
38: super(context);
39: this.field = Objects.requireNonNull(attribute);
40: }
41:
42: @Override
43: public Collection<Descriptor> getAttributeDescriptors() {
44: return Collections.singleton(this);
45: }
46:
47: @Override
48: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
49: return getFieldDescriptor(attribute.getJavaField());
50: }
51:
52: @Override
53: public void addAttributeDescriptor(Field attribute, Descriptor descriptor) {
54: // Do nothing
55: }
56:
57: @Override
58: public void addAttributeContext(Field attribute, URI context) {
59: // Do nothing
60: }
61:
62: /**
63: * Use {@link #setLanguage(String)} instead.
64: */
65: @Override
66: public void setAttributeLanguage(Field attribute, String languageTag) {
67: // Do nothing
68: }
69:
70: private Descriptor getFieldDescriptor(Field field) {
71: if (this.field.equals(field)) {
72: return this;
73: }
74: throw new IllegalArgumentException("This field descriptor does not describe field " + field);
75: }
76:
77: @Override
78: protected Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited) {
79: if (context == null) {
80: return null;
81: }
82: contexts.add(context);
83: visited.add(this);
84: return contexts;
85: }
86:
87: Field getField() {
88: return field;
89: }
90:
91: @Override
92: public int hashCode() {
93: final int prime = 31;
94: int result = super.hashCode();
95: result = prime * result + field.hashCode();
96: return result;
97: }
98:
99: @Override
100: public boolean equals(Object obj) {
101: if (this == obj)
102: return true;
103: if (!super.equals(obj))
104: return false;
105: if (getClass() != obj.getClass())
106: return false;
107: FieldDescriptor other = (FieldDescriptor) obj;
108: return field.equals(other.field);
109: }
110: }